/**
 * remove removeAll没写对
 */

public class SingleLinkedList {
    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val=val;
        }
    }

    //                             ￥ListNode head=new ListNode();不能这样new，因为不知道val
    ListNode head;//直接这样就行     ￥public ListNode head;
    int size=0;//                 ￥public int size=0;

    public void addFirst(int data){
        ListNode node=new ListNode(data);
//        if(head==null){
//            head=node;
//            size++;
//            return;
//        }
        node.next=head;
        head=node;
        size++;
    }

    public void addLast(int data){
        ListNode node=new ListNode(data);
        if(head==null){
            head=node;
            size++;
            return;
        }
        ListNode cur=head;
        while(cur.next!=null){
            cur=cur.next;
        }
        cur.next=node;
        size++;
    }

    public boolean addIndex(int index,int data){
        if(index<0||index>size){
            return false;
        }
        if(index==0){
            addFirst(data);
            return true;
        }
        if(index==size){
            addLast(data);
            return true;
        }
        ListNode node=new ListNode(data);
        ListNode cur=head;
        while(index-1>0){
            cur=cur.next;
            index--;
        }
        node.next=cur.next;
        cur.next=node;
        size++;
        return true;
    }

    public boolean contains(int key){
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }








    public void remove(int key){
        if(head==null){
            return;
        }
        ListNode cur=head;
        if(cur.val==key){
            head=head.next;
            size--;
            return;
        }
        while(cur!=null){
            ListNode curN=cur.next;
            if(curN.val==key){//          ￥这里会可能空指针异常：cur为最后一个节点，cur.next为空，curN.val空指针异常
               cur.next=curN.next;
               size--;
               return;
            }
            cur=curN;
        }
    }
/*
 public void remove(int key) {
        if(head == null) {
            return;
        }
        if(head.val == key) {
            head = head.next;
            listSize--;
            return;
        }
        ListNode cur = findNode(key);
        if(cur == null) {
            System.out.println("没有你要删除的数据");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
        listSize--;
    }
    //找到第一个遇到的key的前一个节点
    //没有 找到 返回null
    private ListNode findNode(int key) {
        ListNode cur = head;
        while (cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
 */

    public void removeAll(int key){
        if(head==null){
            return;
        }
        ListNode cur=head;
        while(cur!=null){
            ListNode curN=cur.next;
            //if(cur.val==key){
            if (head.val == key) {
                head = head.next;
                size--;
            }else{
                if(curN.val==key){
                    cur.next=curN.next;
                    size--;
                }
            }
            cur=curN;;
        }
    }
/*
 public void removeAllKey(int key) {
        if(head == null) {
            return;
        }
        //while(head.val == key) {
         //   head = head.next;
        //} 有可能会出现空指针异常
         ListNode cur = head.next;
        ListNode prev = head;

        while (cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                //cur = cur.next;
                listSize--;
            }else {
                prev = cur;
                //cur = cur.next;
            }
            cur = cur.next;
        }
        if(head.val == key) {
            head = head.next;
            listSize--;
        }
    }
 */





    public int size(){
        return size;
    }

    public void display(){
        ListNode cur=head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }

    public void clear(){
        head=null;
        size=0;
    }
}
